package project.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import project.web.Accessdb;


public class Authenticate extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private Accessdb idc;
	
	public Authenticate() {
		super();
    }
	
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init();
		idc = new Accessdb();
		idc.setDbUrl(config.getInitParameter("dbUrl"));
		idc.setDbUser(config.getInitParameter("dbUser"));
		idc.setDbPass(config.getInitParameter("dbPass"));
		try {
			Class.forName(config.getInitParameter("jdbcDriver"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		PrintWriter out = response.getWriter();

		String login_id = request.getParameter("login_id");
		String login_pw = request.getParameter("login_pw");
		
		String[] user = idc.authorize(login_id, login_pw);
		
		if(user != null && session != null){
			out.print("true");
			session.setAttribute("login", user);
		}
		else{
			out.print("false");
		}
		out.flush();
	}
	
	
}
